<?php

namespace App\Kuafu\CustomPayment\Validate;

use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\CustomPayment\Enum\CustomPaymentShopMappingsEnum;
use App\Kuafu\CustomPayment\Model\CustomPaymentModel;
use App\Kuafu\CustomPayment\Model\CustomPaymentShopMappingsModel;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;

class CustomPaymentShopMappingsValidate extends BaseValidate
{

    /**
     * 获取规则信息
     *
     * @param array $fields
     * @return string[]
     */
    public static function getRules(array $fields = []): array
    {
        $rules = [
            'shopIds' => [
                'required',
                'array',
                function ($attribute, $value, $fail) {
                    $count = Shop::query()->whereIn('id', $value)->count();
                    if ($count !== count($value)) {
                        $fail('The ' . $attribute . ' is invalid.');
                    }
                }
            ],
            'payId'   => ['required', 'int', Rule::exists(CustomPaymentModel::class, 'id')->where('isDeleted', 0)],
            'status'  => 'required|integer|in:' . implode(',', array_keys(CustomPaymentShopMappingsEnum::STATUS_MAP)),
            'id'      => ['required', Rule::exists(CustomPaymentShopMappingsModel::class)],
            'startAt' => 'sometimes|date_format:Y-m-d',
            'endAt'   => 'sometimes|date_format:Y-m-d',
        ];

        return empty($fields) ? $rules : (new Collection($rules))->filter(function ($v, $k) use ($fields) {
            if (in_array($k, $fields)) {
                return true;
            }
            return false;
        })->toArray();
    }


    /**
     * 添加校验
     *
     * @param array $requestParams
     * @return array
     */
    public static function insertValidate(array $requestParams): array
    {
        return self::baseValidate($requestParams, ['shopIds', 'payId']);
    }

    /**
     * 添加校验
     *
     * @param array $requestParams
     * @return array
     */
    public static function statusValidate(array $requestParams): array
    {
        return self::baseValidate($requestParams, ['status', 'id']);
    }

    /**
     * 发送记录
     *
     * @param array $requestParams
     * @return array
     */
    public static function sendPaymentHistoryValidate(array $requestParams): array
    {
        return self::baseValidate($requestParams, ['startAt', 'endAt']);
    }

    /**
     * 已下发记录校验
     *
     * @param array $requestParams
     * @return array
     */
    public static function paymentShopValidate(array $requestParams): array
    {
        return self::baseValidate($requestParams, ['payId']);
    }
}
